/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scuecsz.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 *
 * @author Administrator
 */
public class FileUtil {


    public static String  wholePathDiploma(String folder,String userid,String suffix) {
            return   wholePath(folder,diplomaName(userid,suffix));
    }

    public static String  diplomaName(String userid,String suffix) {
        return userid.concat(StringUtil.DIPLOMA).concat(StringUtil.DOT).concat(suffix);
    }


    public static String getFileNameSuffix(String name) {
        int dotIndex=name.lastIndexOf(StringUtil.DOT);
        return  -1==dotIndex?StringUtil.NULLSTRING:name.substring(dotIndex+1, name.length());
    }
    

    public static String fileName(String name,String suffix) {
        return  name.concat(StringUtil.DOT).concat(suffix);
    }

   public static String  wholePath(String folder,String fileName) {
        return folder.concat(File.separator).concat(fileName);
    }

    //组织 文件夹名称
    public static String subPath(String folderName) {
        return File.separator.concat(folderName);
    }
    public static void closeFileInputStream(FileInputStream fis) {
        if (null != fis) {
            try {
                fis.close();
            } catch (IOException ex) {
            }
        }
    }

    public static void closeByteArrayOutputStream(ByteArrayOutputStream bos) {
        if (null != bos) {
            try {
                bos.close();
            } catch (IOException ex) {
            }
        }
    }

    public static void closeObjectOutputStream(ObjectOutputStream oos) {
        if (null != oos) {
            try {
                oos.close();
            } catch (IOException ex) {
            }
        }
    }

    public static void closeFileOutputStream(FileOutputStream fis) {
        if (null != fis) {
            try {
                fis.close();
            } catch (IOException ex) {
            }
        }
    }

    public static void closeFileReader(FileReader fis) {
        if (null != fis) {
            try {
                fis.close();
            } catch (IOException ex) {
            }
        }
    }

    public static void closeBufferedReader(BufferedReader br) {
        if (null != br) {
            try {
                br.close();
            } catch (IOException ex) {
            }
        }
    }

    public static File getFileObject(String path) {
        return new File(path);
    }

    public static String getFileDatas(String path) throws IOException {
        StringBuffer sb = new StringBuffer();
        File file = getFileObject(path);
        if (file.exists()) {
            if (file.isFile()) {
                String temp = null;
                FileReader fis = null;
                BufferedReader br = null;
                try {
                    fis = new FileReader(file);
                    br = new BufferedReader(fis);
                    while (null != (temp = br.readLine())) {
                        sb = sb.append(temp);
                    }
                } catch (FileNotFoundException ex) {
                    throw new IOException(ex);
                } finally {
                    closeFileReader(fis);
                    closeBufferedReader(br);
                }
            }
        }
        return sb.toString();
    }

    public static byte[] getBytesFromFile(File f) throws IOException {
       byte[] obs=null;
        if (f == null) {
            return obs;
        }
        FileInputStream stream=null ;
        ByteArrayOutputStream out=null;
        try {
           stream = new FileInputStream(f);
           out  = new ByteArrayOutputStream();
            int n;
            while ((n = stream.read()) != -1){
                out.write(n);
            }
            obs=out.toByteArray();            
        } catch (IOException e) {
              throw e;
        } finally {
              closeFileInputStream(stream);
                    closeByteArrayOutputStream(out);
                }
        return obs;
    }

    public static byte[] getBytesFromObject(Serializable obj) throws IOException  {
        byte[] obs=null;
        if (obj == null) {
            return obs;
        }
        ByteArrayOutputStream bos=null;
        ObjectOutputStream oos=null;
          try { bos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(bos);
                oos.writeObject(obj);
                obs=bos.toByteArray();
          }  catch (IOException e) {
                    throw e;
                } finally {
                    closeObjectOutputStream(oos);
                    closeByteArrayOutputStream(bos);
                }
        return obs;
    }


    public static boolean copyFile(File sourceFile, String targetFile) throws IOException {
        return copyFile(sourceFile, getFileObject(targetFile));
    }

    //复制文件
    public static boolean copyFile(File sourceFile, File targetFile) throws IOException {
        boolean success = false;
        if (!targetFile.exists()) {//目标文件或目录存在，说明其上级目录存在,但目标文件不存在先要创建起上级目录
            File parentDir = getFileObject(targetFile.getParent());
            if (!parentDir.exists() || parentDir.isFile()) {
                parentDir.mkdirs();
            }
            targetFile.createNewFile();
        }

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(sourceFile);
            fos = new FileOutputStream(targetFile);
            byte[] buffer = new byte[1024];
            int n = 0;
            while ((n = fis.read()) != -1) {
                fos.write(n);
            }
            success = true;
        } catch (FileNotFoundException e) {
            throw new IOException(e);
        } catch (IOException e) {
            throw e;
        } finally {
            closeFileInputStream(fis);
            closeFileOutputStream(fos);
        }
        return success;
    }
}

